package com.youlai.system.controller.device;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.pojo.entity.device.DeviceWarnLogEntity;

import com.youlai.system.pojo.query.DeviceWarnLogQuery;
import com.youlai.system.pojo.vo.device.DeviceWarnLogPageVO;
import com.youlai.system.service.impl.device.DeviceService;
import com.youlai.system.service.impl.device.DeviceWarnLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

@Tag(name = "10.设备预警日志接口")
@RestController
@RequestMapping("/api/v1/device/warnLog")
@RequiredArgsConstructor
public class DeviceWarnLogController {

    private final DeviceWarnLogService deviceWarnLogService;

    private final DeviceService deviceService;

    @Operation(summary = "告警日志", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<DeviceWarnLogPageVO> page(
            DeviceWarnLogQuery queryParams
    ) {
        LambdaQueryWrapper<DeviceWarnLogEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (queryParams.getDeviceId() != null) {
            lambdaQueryWrapper.eq(DeviceWarnLogEntity::getDeviceId, queryParams.getDeviceId());
        }
        if (queryParams.getType() != null) {
            lambdaQueryWrapper.eq(DeviceWarnLogEntity::getType, queryParams.getType());
        }
        if (queryParams.getStatus() != null) {
            lambdaQueryWrapper.eq(DeviceWarnLogEntity::getStatus, queryParams.getStatus());
        }


        lambdaQueryWrapper.orderByDesc(DeviceWarnLogEntity::getId);
        Page<DeviceWarnLogEntity> page = new Page(queryParams.getPageNum(), queryParams.getPageSize());
        page = deviceWarnLogService.page(page, lambdaQueryWrapper);


        IPage<DeviceWarnLogPageVO> warnLogPageVOIPage = new Page<>();
        warnLogPageVOIPage.setPages(page.getPages());
        warnLogPageVOIPage.setTotal(page.getTotal());

        warnLogPageVOIPage.setRecords(page.getRecords()

                .stream().map(
                        warnLog -> {
                            DeviceWarnLogPageVO pageVO = new DeviceWarnLogPageVO();
                            pageVO.setDeviceName(deviceService.getById(warnLog.getDeviceId()).getName());
                            if ("PM".equals(warnLog.getType())) {
                                pageVO.setType("pm");
                            }
                            if ("TEMP".equals(warnLog.getType())) {
                                pageVO.setType("温度");
                            }
                            if ("YW".equals(warnLog.getType())) {
                                pageVO.setType("液位");
                            }
                            if ("DL".equals(warnLog.getType())) {
                                pageVO.setType("电量");
                            }
                            if ("SIGNAL".equals(warnLog.getType())) {
                                pageVO.setType("信号强度");
                            }
                            pageVO.setConfigValue(warnLog.getConfigValue());
                            pageVO.setRealValue(warnLog.getRealValue());
                            if(1 == warnLog.getStatus()){
                                pageVO.setStatus("未处理");
                            }else if(2== warnLog.getStatus()){
                                pageVO.setStatus("已处理");
                            }
                            pageVO.setTime(new DateTime(warnLog.getCreateTime().getTime())
                                    .toString("yyyy-MM-dd HH:mm:ss"));
                            return pageVO;
                        }
                ).collect(Collectors.toList()));

        return PageResult.success(warnLogPageVOIPage);
    }
}


